package cn.ljlao.srpingevent;


import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: LiuZhen
 * @Created: 2020/9/2 16:38
 * @Description:
 */
public class MyBatisPlusGender {
    public static void main(String[] args) {
        //需要构建一个 代码生成器对象
        AutoGenerator mpg = new AutoGenerator();
        //配置策略
        //1.全局配置
        GlobalConfig gc = new GlobalConfig();
        String path = System.getProperty("user.dir");//获取当前路径
        gc.setOutputDir(path+"/src/main/java");//代码生成的位置
        gc.setAuthor("LiuZhen");//作者名字
        gc.setOpen(false);//是否打开文件资源路径
        gc.setFileOverride(false);//是否覆盖
        gc.setServiceName("%sService");//去Service的I前缀
        gc.setIdType(IdType.ID_WORKER);//Id 的类型
        gc.setDateType(DateType.ONLY_DATE);//日期类型
        gc.setSwagger2(false);//生成swagger
        mpg.setGlobalConfig(gc);

        //2.设置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        //云库
        setDataSourceConfig(dsc);
        //dsc.setUrl("jdbc:mysql://192.168.110.128:3306/PANGU?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        //dsc.setUrl("jdbc:mysql://127.0.0.1:3309/PANGU?useSSL=false&useUnicode=true&characterEncoding=utf-8&serverTimezone=GMT%2B8");
        //dsc.setDriverName("com.mysql.jdbc.Driver");
        //dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        //dsc.setUsername("pangu");
        //dsc.setPassword("pangu");
//        dsc.setUsername("root");
//        dsc.setPassword("root");
        //dsc.setDbType(DbType.MYSQL);
        mpg.setDataSource(dsc);


        //3.包的配置
        PackageConfig pc = new PackageConfig();
        pc.setModuleName("bootdemo");//模块名字
        pc.setParent("cn.ljlao");//在哪个包下
        pc.setEntity("pojo");
        pc.setMapper("mapper");
        //pc.setService("service");
        //pc.setController("controller");
        mpg.setPackageInfo(pc);

        //4.策略配置
        StrategyConfig strategy = new StrategyConfig();
        strategy.setInclude("wf_task_dict");//要映射的表名字
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setEntityLombokModel(true);//自动lombok 注入
        strategy.setLogicDeleteFieldName("deleted");//逻辑删除字段

        //自动填充设置
        TableFill createTime = new TableFill("create_time", FieldFill.INSERT);
        TableFill updateTime = new TableFill("update_time", FieldFill.INSERT_UPDATE);

        List<TableFill> list = new ArrayList<>();
        list.add(createTime);
        list.add(updateTime);
        strategy.setTableFillList(list);
        //乐观锁
        strategy.setVersionFieldName("version");
        strategy.setRestControllerStyle(true);
        strategy.setControllerMappingHyphenStyle(true);
        mpg.setStrategy(strategy);
        mpg.execute();
    }

    public static void setDataSourceConfig(DataSourceConfig dsc){
        dsc.setUrl("jdbc:mysql://192.168.70.200:80/bladex_dev?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true&allowPublicKeyRetrieval=true");
        dsc.setDriverName("com.mysql.jdbc.Driver");
        //dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("root");
        dsc.setPassword("xczg_2021@2022");
        dsc.setDbType(DbType.MYSQL);

    }
}
